package com.ep.common.plugins;

import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.*;

public class ServiceGenerator {
    public void generate(Interface interfaze, TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
        generateServiceInterface(interfaze, topLevelClass, introspectedTable);
        generateServiceImpl(interfaze, topLevelClass, introspectedTable);
    }

//    public void generate(Interface interfaze, TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
//        // 生成服务接口
//        Interface serviceInterface = new Interface("com.ep.modules.app.controller." + controllerName);
//        generateServiceInterface(serviceInterface, topLevelClass, introspectedTable);
//        // 写入服务接口文件
//        CodeGenerationUtils.writeToFile(serviceInterface, "src/main/java/com/ep/modules/app/service", serviceInterface.getType().getShortName() + "Service.java");
//
//        // 生成服务实现类
//        TopLevelClass serviceImplClass = new TopLevelClass();
//        generateServiceImpl(serviceInterface, serviceImplClass, introspectedTable);
//        // 写入服务实现类文件
//        CodeGenerationUtils.writeToFile(serviceImplClass, "src/main/java/com/ep/modules/app/service/impl", serviceImplClass.getType().getShortName() + "ServiceImpl.java");
//    }

    private void generateServiceInterface(Interface interfaze, TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
        // 获取表名和实体类名
        String tableName = introspectedTable.getFullyQualifiedTable().getIntrospectedTableName();
        String entityName =  CodeGenerationUtils.toCamelCase(tableName);
        //String packageName = interfaze.getPackage().getFullyQualifiedName();
        String serviceName = entityName + "Service";
        String serviceNameFirstCharToLower = CodeGenerationUtils.firstCharToLower(serviceName);

        // 设置接口的包名
       // interfaze.setPackage(new FullyQualifiedJavaType(packageName));


        Interface serviceInterface = new Interface("com.ep.modules.app.service." + serviceName);
        serviceInterface.setVisibility(JavaVisibility.PUBLIC);

        serviceInterface.addImportedType(new FullyQualifiedJavaType("com.ep.common.utils.PageUtils"));
        serviceInterface.addImportedType(new FullyQualifiedJavaType("java.util.Map"));
        serviceInterface.addImportedType(new FullyQualifiedJavaType("com.ep.modules.app.entity." + entityName));
        //serviceInterface.addImportedType(new FullyQualifiedJavaType("java.util.List"));

        // 添加分页查询方法
        Method queryPageMethod = new Method("queryPage");
        queryPageMethod.setReturnType(new FullyQualifiedJavaType("PageUtils"));
        queryPageMethod.addParameter(new Parameter(new FullyQualifiedJavaType("Map<String, Object>"), "params"));
        serviceInterface.addMethod(queryPageMethod);

        //getById
        Method getByIdMethod = new Method("getById");
        getByIdMethod.setReturnType(new FullyQualifiedJavaType(entityName));
        getByIdMethod.addParameter(new Parameter(new FullyQualifiedJavaType("Long"), "id"));
        serviceInterface.addMethod(getByIdMethod);

        // 添加保存方法
        Method saveMethod = new Method("save");
        saveMethod.addParameter(new Parameter(new FullyQualifiedJavaType(entityName), "entity"));
        serviceInterface.addMethod(saveMethod);

        // 添加更新方法
        Method updateMethod = new Method("update");
        updateMethod.addParameter(new Parameter(new FullyQualifiedJavaType(entityName), "entity"));
        serviceInterface.addMethod(updateMethod);

        // 添加批量删除方法
        Method deleteBatchMethod = new Method("deleteBatch");
        deleteBatchMethod.addParameter(new Parameter(new FullyQualifiedJavaType("Long[]"), "ids"));
        serviceInterface.addMethod(deleteBatchMethod);

        // 写入服务接口文件
       CodeGenerationUtils.writeToFile(serviceInterface, "src/main/java/com/ep/modules/app/service", serviceName + ".java");
    }

    private void generateServiceImpl(Interface interfaze, TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
        // 获取表名和实体类名
        String tableName = introspectedTable.getFullyQualifiedTable().getIntrospectedTableName();
        String entityName = CodeGenerationUtils.toCamelCase(tableName);
        String serviceName = entityName + "Service";
        String serviceNameFirstCharToLower = CodeGenerationUtils.firstCharToLower(serviceName);

        String serviceImplName = entityName + "ServiceImpl";
        String daoName = entityName + "Mapper";
        String daoNameFirstCharToLower = CodeGenerationUtils.firstCharToLower(daoName);
        String serviceImplNameFirstCharToLower = CodeGenerationUtils.firstCharToLower(serviceImplName);

        // 设置实现类的包名
        TopLevelClass serviceImplInterface = new TopLevelClass("com.ep.modules.app.service.impl." + serviceImplName);
        serviceImplInterface.setVisibility(JavaVisibility.PUBLIC);
        serviceImplInterface.addAnnotation("@Service");

        // 添加实现的接口
        FullyQualifiedJavaType serviceInterfaceType = new FullyQualifiedJavaType("com.ep.modules.app.service." + entityName + "Service");
        serviceImplInterface.addSuperInterface(serviceInterfaceType);
        serviceImplInterface.addImportedType(serviceInterfaceType);

        // 添加相关依赖
        serviceImplInterface.addImportedType("com.ep.modules.app.dao." + daoName);
        serviceImplInterface.addImportedType("com.ep.modules.app.entity." + entityName);
        serviceImplInterface.addImportedType("com.ep.modules.app.entity." + entityName + "Example");
        serviceImplInterface.addImportedType("com.ep.common.utils.PageUtils");
        //serviceImplInterface.addImportedType("com.ep.modules.app.entity.SysMenuExample");
        serviceImplInterface.addImportedType("org.apache.commons.lang.StringUtils");
        serviceImplInterface.addImportedType("org.springframework.beans.factory.annotation.Autowired");
        serviceImplInterface.addImportedType("org.springframework.stereotype.Service");
        serviceImplInterface.addImportedType("java.util.List");
        serviceImplInterface.addImportedType("java.util.Map");
        serviceImplInterface.addImportedType("com.baomidou.mybatisplus.extension.plugins.pagination.Page");

        // 添加服务注入
        Field serviceField = new Field(daoNameFirstCharToLower, new FullyQualifiedJavaType(daoName));
        serviceField.setVisibility(JavaVisibility.PRIVATE);
        serviceField.addAnnotation("@Autowired");
        serviceImplInterface.addField(serviceField);

        // 添加分页查询方法实现
        Method queryPageMethod = new Method("queryPage");
        queryPageMethod.setVisibility(JavaVisibility.PUBLIC);
        queryPageMethod.setReturnType(new FullyQualifiedJavaType("PageUtils"));
        queryPageMethod.addParameter(new Parameter(new FullyQualifiedJavaType("Map<String, Object>"), "params"));
        queryPageMethod.addBodyLine("String searchKey = (String)params.get(\"searchKey\");");
        queryPageMethod.addBodyLine("Long searchUserId = (Long)params.get(\"searchUserId\");");
        queryPageMethod.addBodyLine(entityName + "Example example = new "+ entityName +"Example();");
        queryPageMethod.addBodyLine(entityName + "Example.Criteria criteria = example.createCriteria();");
//        queryPageMethod.addBodyLine("if (StringUtils.isNotBlank(searchKey)) {");
//        queryPageMethod.addBodyLine("    criteria.andNameLike(\"%\" + searchKey + \"%\");");
//        queryPageMethod.addBodyLine("}");
        queryPageMethod.addBodyLine("List<"+ entityName +"> list = " + daoNameFirstCharToLower + ".selectByExample(example);");
        queryPageMethod.addBodyLine("Page<"+ entityName +"> page = new Page<>();");
        queryPageMethod.addBodyLine("page.setRecords(list);");
        queryPageMethod.addBodyLine("return new PageUtils(page);");
        serviceImplInterface.addMethod(queryPageMethod);

        // getById
        Method getByIdMethod = new Method("getById");
        getByIdMethod.setVisibility(JavaVisibility.PUBLIC);
        getByIdMethod.setReturnType(new FullyQualifiedJavaType(entityName));
        getByIdMethod.addParameter(new Parameter(new FullyQualifiedJavaType("Long"), "id"));
        getByIdMethod.addBodyLine("return " + daoNameFirstCharToLower + ".selectByPrimaryKey(id.intValue());");
        serviceImplInterface.addMethod(getByIdMethod);

        // 添加保存方法实现
        Method saveMethod = new Method("save");
        saveMethod.setVisibility(JavaVisibility.PUBLIC);
        saveMethod.addParameter(new Parameter(new FullyQualifiedJavaType(entityName), "entity"));
        saveMethod.addBodyLine(daoNameFirstCharToLower + ".insert(entity);");
        serviceImplInterface.addMethod(saveMethod);

        // 添加更新方法实现
        Method updateMethod = new Method("update");
        updateMethod.setVisibility(JavaVisibility.PUBLIC);
        updateMethod.addParameter(new Parameter(new FullyQualifiedJavaType(entityName), "entity"));
        updateMethod.addBodyLine(daoNameFirstCharToLower + ".updateByPrimaryKeySelective(entity);");
        serviceImplInterface.addMethod(updateMethod);

        // 添加批量删除方法实现
        Method deleteBatchMethod = new Method("deleteBatch");
        deleteBatchMethod.setVisibility(JavaVisibility.PUBLIC);
        deleteBatchMethod.addParameter(new Parameter(new FullyQualifiedJavaType("Long[]"), "ids"));
        deleteBatchMethod.addBodyLine("for (Long id : ids) {");
        deleteBatchMethod.addBodyLine("    " + daoNameFirstCharToLower + ".deleteByPrimaryKey(id.intValue());");
        deleteBatchMethod.addBodyLine("}");
        serviceImplInterface.addMethod(deleteBatchMethod);

        // 写入服务接口文件
        CodeGenerationUtils.writeToFile(serviceImplInterface, "src/main/java/com/ep/modules/app/service/impl", serviceImplName + ".java");
    }
}
